import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:furaha_mkopo/cache/fm_cache_tool.dart';
import 'package:furaha_mkopo/commonTool/fm_route_tool.dart';
import 'package:furaha_mkopo/page/tab/fm_tab_page.dart';
import 'package:furaha_mkopo/welcome/fm_welcome_cell.dart';

class FMWelcomePage extends StatefulWidget {

  const FMWelcomePage({super.key});

  @override
  State<FMWelcomePage> createState() => _FMWelcomePageState();
}

class _FMWelcomePageState extends State<FMWelcomePage> {

  final PageController _pageController = PageController();

  List<String> list = [
    "welcome_1","welcome_2"
  ];

  int selectIndex = 0;

  @override
  void initState() {
    super.initState();

    _pageController.addListener(() {
      setState(() {
        selectIndex = _pageController.page!.round(); // 获取当前页面索引
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.white,
      child: PageView.builder(
        itemCount: list.length,
        controller: _pageController,
        itemBuilder: (context,index) {
           return FMWelcomeCell(
             imageName: list[index],
             isLast: selectIndex == list.length - 1,
             onTap: (){
               onClick();
             },
           );
        }
      ),
    );
  }


  void onClick() {
    if (selectIndex == list.length - 1) {
      jumpHomeController();
    }else {
      selectIndex += 1;
      _pageController.animateToPage(
        selectIndex,
        duration: const Duration(milliseconds: 300),
        curve: Curves.easeInOut,
      );
    }
  }

  void jumpHomeController() {
    FMRouteTool.push(context, const FMTabPage());
    FMCacheTool.saveWelcomePageFlag(true);
  }
}
